package gao.xiaolei.dao;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface TreeMethod<T> {


    // 根据节点id和状态和父节点Id来查找
    public List<T> findByProjectAndStatusAndParentId(String projectId, Integer status, String parentId);

    public List<T> findByProjectAndStatus(String project, Integer status);

    // 根据项目id和父节点Id来查找测试计划的菜单字段（仅查找根节点）
    @Query(value = "select id,title,parentId,leaf from #{#entityName} where project = :projectId and parentId=:parentId order by title")
    public List<Object[]> findByProjectAndParentIdOrderByTitleForTransform(@Param("projectId") String projectId,
                                                                           @Param("parentId") String parentId);

    // 获取节点的状态
    @Query(value = "select status from #{#entityName} where id=:id")
    public Integer getStatus(@Param("id") String id);

    // 获取该节点的能显示的节点（自己写的或则别人审核通过的或者是文件夹）
    @Query(value = "select id,title,parentId,leaf,author,status from #{#entityName} where parentId = :id and(author=:userId or status=3 or leaf=0)")
    public List<Object[]> getChildren(@Param("id") String id,
                                      @Param("userId") String userId);
    //根据项目和父Id获取节点数量
    public Integer countByProjectAndParentId(String projectId, String parentId);

    // 获取该节点的子节点数量
    @Query(value = "select count(*) from #{#entityName} where parentId = :id")
    public Integer getChildrenCount(@Param("id") String id);

    // 获取该节点的子主题文件夹数量
    @Query(value = "select count(*) from #{#entityName} where parentId = :id and leaf=0")
    public Integer getChildrenCountIsFolder(@Param("id") String id);

    // 获取该节点的子节点（用于副本查询）
    @Query(value = "select id,title,parentId,leaf from #{#entityName} where parentId = :id")
    public List<Object[]> getChildren(@Param("id") String id);

    // 获取该节点的能显示的节点（审核通过的或者是文件夹）
    @Query(value = "select id,title,parentId,leaf from #{#entityName} where parentId = :id and(status=3 or leaf=0)")
    public List<Object[]> getChildrenIsPass(@Param("id") String id);

    // 获取该节点的子节点数量（审核通过的或者是文件夹）
    @Query(value = "select count(*) from #{#entityName}  where parentId = :id and(status=3 or leaf=0)")
    public Integer getChildrenCountIsPass(@Param("id") String id);

    // 获取该节点下的主题文件夹
    @Query(value = "select id,title,parentId,leaf from #{#entityName} where parentId = :id and leaf=0")
    public List<Object[]> getChildrenIsFolder(@Param("id") String id);

    // 获得该节点的父需求Id
    @Query(value = "select parentId from #{#entityName} where id = :id")
    public String getParentId(@Param("id") String id);

    // 获得该节点的leaf值
    @Query(value = "select leaf from #{#entityName} where id = :id")
    public Integer getLeaf(@Param("id") String id);

    // 获取该节点的名称
    @Query(value = "select title from #{#entityName} where id = :id")
    public String getTitle(@Param("id") String id);

    // 获取节点的名称和projectId
    @Query("select title,project from #{#entityName} where id=:id")
    public List<Object[]> getTitleAndProject(@Param("id") String id);

    // 更改节点的leaf
    @Modifying
    // update或者delete操作，在更新或者删除操作时，此注解必须加
    @Query(value = "update #{#entityName} set leaf = :leaf where id = :id")
    public void updateLeaf(@Param("id") String id, @Param("leaf") int leaf);

    // 查找该节点的所有子需求Id
    @Query(value = "select id from #{#entityName} where parentId = :parentId")
    public List<String> getChildrenId(@Param("parentId") String parentId);

    // 更改节点的状态
    @Modifying
    // update或者delete操作，在更新或者删除操作时，此注解必须加
    @Query(value = "update #{#entityName} set status = :status where id = :id")
    public void updateStatus(@Param("id") String id, @Param("status") int status);

    // 获取测试需求的作者
    @Query(value = "select author from #{#entityName} where id=:id")
    public Optional<String> getAuthor(@Param("id") String id);

    // 更改节点的标题（用于更新菜单显示）
    @Modifying
    // update或者delete操作，在更新或者删除操作时，此注解必须加
    @Query(value = "update #{#entityName} set title = :title where id = :id")
    public void updateTitle(@Param("id") String id, @Param("title") String title);

    // 更改节点的父节点
    @Modifying
    // update或者delete操作，在更新或者删除操作时，此注解必须加
    @Query(value = "update #{#entityName} set parentId = :parentId where id = :id")
    public void updateParentId(@Param("id") String id,
                               @Param("parentId") String parentId);
    // 获取该节点的创建日期
    @Query(value = "select createDate from #{#entityName} where id = :id")
    public LocalDateTime getCreateDate(@Param("id") String id);

    // 根据项目id和父节点Id来查找节点的菜单字段（一般仅用于查找根节点）
    @Query(value = "select id,title,parentId,leaf,author,status from #{#entityName} where project = :projectId and parentId=:parentId order by title")
    public List<Object[]> findByProjectAndParentIdOrderByTitle(@Param("projectId") String projectId,
                                                               @Param("parentId") String parentId);
    @Query("select id from #{#entityName} where project = :projectId")
    public List<String> findIdsByProject(@Param("projectId") String projectId);

    @Modifying
    public void deleteAllByProject(String projectId);

}
